package com.jb.repair.util;

import android.os.Environment;
import android.os.StatFs;

import com.jb.repair.common.AppConfig;

import java.io.File;

/**
 * Created by gaobin on 2015/12/10.
 */
public class MemoryUtil {
    /**
     * SDCARD是否存在
     */
    public static boolean externalMemoryAvailable() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取SDCARD剩余存储空间
     *
     * @return
     */
    public static long getAvailableExternalMemorySize(String path) {
        StatFs stat = new StatFs(path);
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        Long size = availableBlocks * blockSize;
        return size;
    }

    public static long getAvailableExternalMemorySize() {
        StatFs stat = new StatFs(AppConfig.BASE_PATH);
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        Long size = availableBlocks * blockSize;
        return size;
    }

    public static String getBasePath() {
        String path;
        int sdkVersion = Integer.parseInt(android.os.Build.VERSION.SDK);
        if (sdkVersion < 19) {
            if (new File("/storage/extSdCard").exists() && getAvailableExternalMemorySize("/storage/extSdCard") > 0) {
                path = "/storage/extSdCard";
            } else if ((new File("/storage/sdcard1").exists()) && getAvailableExternalMemorySize("/storage/sdcard1") > 0) {
                path = "/storage/sdcard1";
            } else {
                path = Environment.getExternalStorageDirectory().getPath();
            }
        }else
            path = Environment.getExternalStorageDirectory().getPath();
        return path;
    }
}